/**
 * 
 */
package com.wisdytech.linkdcs.energy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.energy.dao.IEqEnergyDao;
import com.wisdytech.linkdcs.equipment.model.EquipModel;
import com.wisdytech.linkdcs.energy.model.EquipmentEnergy;
import com.wisdytech.linkdcs.energy.service.IEquipmentEnergyService;

/**
 * <p>
 * ClassName: 
 * </p>
 * <p>
 * Description: 
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-10-12
 * </p>
 */
@Service("equipmentEnergyService")
@Transactional
public class EquipmentEnergyServiceImpl extends BaseService<IEqEnergyDao, EquipmentEnergy> implements IEquipmentEnergyService{
    /**
     * 注入设备能耗dao
     */
    @Resource
    private IEqEnergyDao energyDao;
    
    @Override
    public PageInfo<EquipmentEnergy> listEquipmentEnergyByFilter(String code, String type, Filter filter) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("code", code);
        queryMap.put("type", type);
        List<EquipmentEnergy> lists =
                super.beforeList(IEqEnergyDao.class, filter).selectParamPage(queryMap);
        PageInfo<EquipmentEnergy> equipEnergyPage=new PageInfo<>(lists);
        return equipEnergyPage;
    }

    @Override
    public StandardResult addEquipmentEnergy(EquipmentEnergy energy, String loginName) {
        //验证能耗设备编号的唯一性
        EntityWrapper<EquipmentEnergy> ew =new EntityWrapper<>();
        List<EquipmentEnergy> energyList;
        energyList = energyDao.selectPage(new Page<EquipModel>(1, 1), ew.eq("code", energy.getCode()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!energyList.isEmpty()) {
            return StandardResult.getFailResult(CommonConstants.EQUIPMENT_ENERGY);
        }
        energy.setPkId(BaseUtil.getUUID());
        energy.setDelFlag(CommonConstants.DEL_FLAG);
        energy.setOperationId(energy.getOperationName());
        energy.setCreateDate(new Date());
        energy.setCreateUser(loginName);
        int energyCount = energyDao.insert(energy);
        if (energyCount<=0){
            throw new ServiceException("新增失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult delEquipmentEnergyById(String id) {
        List<String> idList = new ArrayList<String>();
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            idList.add(str[i].trim());
        }
        int energyCount = energyDao.deleteBatchIds(idList);
        if (energyCount <= 0) {
            throw new ServiceException("删除失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateEquipmentEnergy(EquipmentEnergy energy, String loginName) {
        EntityWrapper<EquipmentEnergy> ewEnergy = new EntityWrapper<>();
        List<EquipmentEnergy> eqEnergyList = new ArrayList<EquipmentEnergy>();

        eqEnergyList = energyDao.selectList(ewEnergy.ne("pk_id", energy.getPkId()).eq("code", energy.getCode()));
        if (eqEnergyList.size() > 0 ) {
            return StandardResult.getFailResult(CommonConstants.EQUIPMENT_ENERGY);
        }
        energy.setOperationId(energy.getOperationName());
        energy.setUpdateDate(new Date());
        energy.setUpdateUser(loginName);
        int energyCount = energyDao.updateById(energy);
        if (energyCount<=0){
            throw new ServiceException("修改失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public EquipmentEnergy getEquipmentEnergyById(String pkId) {
       EquipmentEnergy energy = energyDao.getEquipmentEnergyById(pkId);
       return energy;
    }

    @Override
    public List<EquipmentEnergy> listAllEquipmentEnergy(String search) {
        EntityWrapper<EquipmentEnergy> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            ew.like("code", search);
        }
        List<EquipmentEnergy> energyList = energyDao.selectList(ew);
        return energyList;
    }

}
